#include <stdexcept>
#include <iostream>
class FileHandle {
    FILE* file;
public:
    FileHandle(const char* filename, const char* mode) {
        file = fopen(filename, mode);
        if (!file) {
            throw std::runtime_error("Failed to open file.");
        }
    }

    ~FileHandle() {
        if (file) fclose(file);
    }

    // 禁止复制
    FileHandle(const FileHandle&) = delete;
    FileHandle& operator=(const FileHandle&) = delete;

    // 支持移动
    FileHandle(FileHandle&& other) noexcept : file(other.file) {
        other.file = nullptr;
    }
    FileHandle& operator=(FileHandle&& other) noexcept {
        if (this != &other) {
            fclose(file);
            file = other.file;
            other.file = nullptr;
        }
        return *this;
    }

    // 文件操作方法
    size_t read(void* buffer, size_t size, size_t count) {
        return fread(buffer, size, count, file);
    }
};
int main() {
    try {
        FileHandle file("test.txt", "r");
        char buffer[1024];
        size_t bytesRead = file.read(buffer, 1, sizeof(buffer));
        std::cout << "Read " << bytesRead << " bytes." << std::endl;
    } catch (const std::exception& e) {
        std::cerr << "Exception: " << e.what() << std::endl;
    }
    return 0;
}